<template>
	<view :class="currentTheme">
		<a-nabbar name="邀请玩家" />
		<mescroll-body ref="mescrollRef" @init="mescrollInit"  @up="upCallback"  @down="downCallback">
			<view class="u-padding-32">
				<!-- <item-search placeholder="请输入玩家名" @change="downCallback" v-model="form.nickname"></item-search> -->
				<view class="u-font-20 text-order-time u-padding-20 u-text-center">以下玩家偏好与发车剧本匹配，邀请Ta来组局吧！</view>
				<view class="u-margin-bottom-20"  v-for="(item,index) in dataList" :key="index">
					<item-box padding="20">
						<userHeader :form="item" showLV>
							<view class="">
								<item-btn @click="shareItem(item)" width="166" height="70"><text class="u-font-24">邀请组局</text></item-btn>
							</view>
						</userHeader>
						<view class="u-flex">
							<view style="width: 100rpx;height: 100rpx;"></view>
							<view class="u-flex-1">
								<userLevel :item="item.userGameLevel" v-if="item.userGameLevel"></userLevel>
								<view class="u-padding-left-40">
									<view class="box-title u-font-20">剧本偏好：</view>
									<view class="u-flex u-margin-top-20 u-flex-wrap">
										<u-row>
											<u-col :span="4" v-for="(c,i) in item.userPreferenceList" :key="i">
												<view class="u-flex  u-margin-bottom-28">
													<item-btn width="152" height="56" bgColor="#EFEFEF">
														<text class="u-font-24 content-desc-item">{{c.categoryName}}</text>
													</item-btn>
												</view>
											</u-col>
										</u-row>
									</view>
								</view>
							</view>
						</view>
					</item-box>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import upCallback from '@/mixins/upCallbackMixin.js'
	import itemSearch from '@/components/itemSearch/itemSearch.vue'
	let WebIM = require("@/utils/WebIM")["default"];
	let msgType = require("@/components/chat/msgtype.js");
	let msgStorage = require("@/components/chat/msgstorage");
	import userHeader from '@/components/userHeader/userHeader.vue'
	import userLevel from '@/components/userHeader/userLevel.vue'
	export default {
		components: {
			userHeader,userLevel,itemSearch
		},
		mixins:[upCallback],
		data() {
			return {
				form:{
					nickname:''
				},
				apiType:'product',
				name:'getMatchGameUsers',
				apiModule:'shopApi'
			};
		},
		onLoad(option) {
			option.userType='player';
			this.form = {...this.form,...option};
			this.form.productId = option.productId;
			this.form.createName = this.loginForm.nickname
			this.form.gatherGameId = option.teamId;
			const {hxPassword,hxUsername} = this.loginForm;
			getApp().globalData.login({hxPassword,hxUsername})
		},
		computed: {
			loginForm() {
				return this.$store.getters.loginForm
			}
		},
		methods: {
			shareItem(item){
				const me = this;
				let id = WebIM.conn.getUniqueId();
				const chatmsgtype = msgType.CUSTOM;
				let msg = new WebIM.message(chatmsgtype, id);
				msg.set({
				  from:this.loginForm.hxUsername,
				  customEvent:"CUSTOM",
				  customExts:this.form,
				  to:item.hxUsername,
				  roomType: false,
				  chatType:'singleChat',
				  success(id, serverMsgId) {
				    console.log("成功了");
					me.$u.toast('邀请成功')
				    disp.fire("em.chat.sendSuccess", id, me.textMsg);
				  },
				  fail(id, serverMsgId) {
					me.$u.toast('邀请失败')
				    console.log("失败了",item.hxUsername,me.loginForm.hxUsername);
				  },
				});
				try {
					me.saveMessage(msg,chatmsgtype)
				} catch (error) {
					me.$u.toast('邀请失败')
				  console.log("error", error);
				}
			},
			//保存消息
			saveMessage(msg,type){
				WebIM.conn.send(msg.body);
				msgStorage.saveMsg(msg, type);
			},
		},
	}
</script>

<style lang="scss" scoped>
	.history-item{
		border-right: 1px solid #9B9CC0;
	}
</style>
